package nil.ed.simulator.request.biz.qq.listener;

import com.google.common.collect.Maps;
import com.google.inject.Inject;
import lombok.Setter;
import nil.ed.simulator.request.biz.qq.scheduler.QqLoginScheduleUnit;
import nil.ed.simulator.request.client.activity.ActivityEngine;
import nil.ed.simulator.request.client.activity.BuiltInAttrs;
import nil.ed.simulator.request.common.QQItem;
import nil.ed.simulator.request.enums.ActivityGroup;
import nil.ed.simulator.request.enums.events.EventMode;
import nil.ed.simulator.request.enums.events.EventType;
import nil.ed.simulator.request.event.Event;
import nil.ed.simulator.request.event.EventBusAware;
import nil.ed.simulator.request.event.bus.EventBus;
import nil.ed.simulator.request.event.bus.EventListener;
import nil.ed.simulator.request.repo.RuntimeRepo;
import nil.ed.simulator.request.scheduler.Scheduler;
import org.apache.http.client.CookieStore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author lidelin.
 */
public class StartActivityListener implements EventListener, EventBusAware {

    private final ActivityEngine engine;
    private final Scheduler scheduler;
    @Setter
    private EventBus eventBus;

    @Inject
    public StartActivityListener(ActivityEngine engine,
                                 Scheduler scheduler) {
        this.engine = engine;
        this.scheduler = scheduler;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void onEvent(Event event) {
        List<QQItem> itemList = RuntimeRepo.QQ_REPO.getQqList();
        RuntimeRepo.QQ_REPO.setFinished(new CopyOnWriteArrayList<>());
        RuntimeRepo.QQ_REPO.setAllUnits(new CopyOnWriteArrayList<>());
        for (QQItem item : itemList) {
            QqLoginScheduleUnit scheduleUnit = new QqLoginScheduleUnit(item, () -> {
                Map<String, Object> context = Maps.newHashMap();
                context.put("qq", item.getQq());
                context.put("password", item.getPassword());
                context.put(BuiltInAttrs.ENTITY_ATTR, item);
                engine.process(ActivityGroup.QQ_LOGIN, context);
                CookieStore cookieStore = (CookieStore) context.get(BuiltInAttrs.COOKIE_STORE_ATTR);
                if (!context.containsKey("need.vc")) {
                    if (context.containsKey("login.message") && String.valueOf(context.get("login.message")).contains("登录成功")) {
                        Map<String, Object> extendMap = new HashMap<>(4, 1);
                        extendMap.put("entity", item);
                        eventBus.publish(Event.builder()
                                .source(null)
                                .eventMode(EventMode.SYNC)
                                .data(context)
                                .eventType(EventType.LOGIN_SUCCESS)
                                .build(),
                                Event.builder()
                                .source(null)
                                .eventMode(EventMode.SYNC)
                                .data(cookieStore)
                                .extendMap(extendMap)
                                .eventType(EventType.COOKIE_STORE)
                                .build());
                    } else {
                        eventBus.publish(Event.builder()
                                .source(null)
                                .eventMode(EventMode.SYNC)
                                .data(context)
                                .eventType(EventType.LOGIN_FAILED)
                                .build());
                    }
                }
                return context;
            });
            scheduler.addScheduleUnit(scheduleUnit);
            RuntimeRepo.QQ_REPO.getAllUnits().add(scheduleUnit);
        }
    }

    @Override
    public boolean isSupport(Event event) {
        return event.getEventType() == EventType.START_ACTIVITY;
    }
}
